package cn.com.cetccst.appstore.tools;

import android.content.Context;
import android.content.Intent;

import cn.com.cetccst.appstore.database.model.AppModel;
import cn.com.cetccst.appstore.manager.ApkDownloadManager;
import cn.com.cetccst.appstore.ui.activity.PlatformWebActivity;
import cn.com.cetccst.tools.pm.AppPackage;

public class AppAction {
    public static void buttonClick(Context context, AppModel appModel) {
        switch (appModel.getStatus()) {
            case STATUS_UNDOWNLOADED -> {
                ApkDownloadManager.getInstance().requestDownLoadApk(context, appModel.getAppId());
            }
            case STATUS_INSTALLED -> {
                if (appModel.isNeedUpgrade()) {
                    ApkDownloadManager.getInstance().requestDownLoadApk(context, appModel.getAppId());
                    return;
                }
                if (AppPackage.isWebApp(appModel.getAppSummary().getAppPackageName())) {
                    Intent intent = new Intent(context, PlatformWebActivity.class);
                    intent.putExtra(PlatformWebActivity.URL_EXTRA, appModel.getOriginalAppId());
                    context.startActivity(intent);
                } else {
                    AppPackage.launchAppByPackageName(context, appModel.getAppSummary().getAppPackageName());
                }
            }
            case STATUS_DOWNLOADED ->
                    ApkDownloadManager.getInstance().installDownloadedApk(context, appModel.getAppId());
            case STATUS_DOWNLOADING ->
                    ApkDownloadManager.getInstance().cancelDownload(context, appModel.getAppId());
        }
    }
}
